package indi.woniu.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import indi.woniu.entity.PerPage;
import indi.woniu.entity.Question;
import indi.woniu.entity.QuestionDTO;
import indi.woniu.entity.ResponseEntity;
import indi.woniu.service.IssueService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/issue")
@CrossOrigin
public class IssueController {
    @Autowired
    private IssueService issueService;
    //测试查询的所有
    @RequestMapping("/findNew")
    public ResponseEntity findNew() {
        List<QuestionDTO> questionDTOS = issueService.findAll();
        return new ResponseEntity<List<QuestionDTO>>(200,"查询所有的数据",questionDTOS);
    }
    //增加一个题目，先增加一个答案记录，拿到答案的id，需要增加该题，然后拿到试题id增加中间表，根据科目年级拿到子题库id，
    @RequestMapping("/addOne")
    public ResponseEntity<Integer> addOne(@RequestBody QuestionDTO questionDTO) {
        return new ResponseEntity<Integer>(200,"添加一个题目成功",issueService.addOne(questionDTO));
    }

    //删除一个题目，需要删除该题与题库的连接，还需要删除自己的记录
    @DeleteMapping("delOne/{id}")
    public ResponseEntity<Integer> delOne(@PathVariable int id) {
        return new ResponseEntity<Integer>(200,"删除一个题目成功",issueService.delOne(id));
    }


    //多表查询题目
    @RequestMapping("/findByCond")
    public ResponseEntity<PageInfo<QuestionDTO>> findByCond(@RequestBody PerPage<QuestionDTO> perPage){
//        PageHelper.startPage(perPage.getPage(), perPage.getLimit());
        List<QuestionDTO> questionDTOS = null;
        //如果科目池子和年级池子为空，就直接从question表中查询其他三个条件findByCondition,加上分页
        if(perPage.getData().getPool().getPoolName().equals("")&&perPage.getData().getSonPool().getPoolName().equals("")){
            return issueService.findByCondition(perPage);
        }else {
            //否则就根据下面的条件查询3+1、3+2
            return issueService.findByCond(perPage);
        }
//        PageInfo<QuestionDTO> questionDTOPageInfo = new PageInfo<>(questionDTOS);
//        return new ResponseEntity<PageInfo<QuestionDTO>>(200,"数据查询成功",questionDTOPageInfo);
    }

    //更改题目,
    @RequestMapping("/updateIssue")
    public ResponseEntity<Integer> updateIssue(@RequestBody QuestionDTO questionDTO){
        return new ResponseEntity<Integer>(200,"数据修改成功",issueService.updateOne(questionDTO));
    }

    //根据id去查询该题的信息
    @PostMapping("/findIssueById/{id}")
    public ResponseEntity<QuestionDTO> findIssueById(@PathVariable int id){
        return new ResponseEntity<QuestionDTO>(200,"数据查询成功",issueService.findById(id));
    }
}